package com.pew.tasktimer.store;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.pew.tasktimer.domain.Task;
import com.pew.tasktimer.domain.TaskTimerPackage;

public class JAXBStoreTest {

	private static Logger cLogger = Logger.getLogger(JAXBStoreTest.class);
	
	private static String cStrTestFile="c:/Temp/test.xml";
	
	@BeforeClass 
	public void setupTest(){
		BasicConfigurator.configure();
	}
	
  @Test
  public void storeXml() {
	  Task lTask = new Task();
	  lTask.startTimer();
	  lTask.setcStrNote("Test timer.");
	  if (cLogger.isDebugEnabled()) {
		cLogger.debug(lTask.toString());
	}
	  
	  JAXBStore.storeXml(lTask, cStrTestFile);
  }
  
  @Test
  public void storeXmlPackage() {
	  Task lTask = new Task();
	  lTask.startTimer();
	  lTask.setcStrNote("Started and stopped timer.");
	  lTask.stopTimer(lTask.getClStartTime() + 120000);
	  
	  TaskTimerPackage lTTP = new TaskTimerPackage();
	  lTTP.addTask(lTask);
	  
	  lTask = new Task();
	  lTask.setcStrNote("Timer not stopped.");
	  lTask.startTimer();
	  
	  lTTP.addTask(lTask);
	  
	  JAXBStore.storeXml(lTTP, cStrTestFile);
  }
  
  @Test
  public void storeAndRead() {
	  Task lTask = new Task();
	  lTask.startTimer();
	  lTask.setcStrNote("Started and stopped timer.");
	  lTask.stopTimer(lTask.getClStartTime() + 120000);
	  
	  TaskTimerPackage lTTP = new TaskTimerPackage();
	  lTTP.addTask(lTask);
	  
	  lTask = new Task();
	  lTask.setcStrNote("Timer not stopped.");
	  lTask.startTimer();
	  
	  lTTP.addTask(lTask);
	  
	  JAXBStore.storeXml(lTTP, cStrTestFile);
	  
	  TaskTimerPackage lNewTTP = JAXBStore.readXmlPackage(cStrTestFile);
	  
	  Task lTaskOld[] = lTTP.getTasks();
	  Task lTaskNew[] = lNewTTP.getTasks();
	  
	  assert (lTaskOld.length == lTaskNew.length);
	  
	  boolean lbFound = false;
	  for(int i = 0; i < lTaskOld.length; i++)
	  {
		  for(int j = 0; j < lTaskNew.length; j++)
		  {
			  if (cLogger.isDebugEnabled()) {
				cLogger.debug("Working on i:j " + i + ":" +j);
				cLogger.debug(lTaskOld[i].toString());
				cLogger.debug(lTaskNew[j].toString());
			}
			  if(lTaskOld[i].toString().equals(lTaskNew[j].toString()))
			  {
				  lbFound = true;
				  if (cLogger.isDebugEnabled()) {
					cLogger.debug("Found at i:j " + i + ":" +j);
				}
			  }
		  }
		  
		  assert lbFound;
		  lbFound = false;
	  }
  }
  
  @Test
  public void storeReadChangeStoreRead() {
	  Task lTask = new Task();
	  lTask.startTimer();
	  lTask.setcStrNote("Started and stopped timer.");
	  lTask.stopTimer(lTask.getClStartTime() + 120000);
	  
	  TaskTimerPackage lTTP = new TaskTimerPackage();
	  lTTP.addTask(lTask);
	  
	  lTask = new Task();
	  lTask.setcStrNote("Timer not stopped.");
	  lTask.startTimer();
	  
	  lTTP.addTask(lTask);
	  
	  JAXBStore.storeXml(lTTP, cStrTestFile);
	  
	  TaskTimerPackage lNewTTP = JAXBStore.readXmlPackage(cStrTestFile);
	  
	  lTask = lNewTTP.getLatestRunningTask();
	  if(lTask == null) {
		  assert false;
		  // we know there is on running.
	  }
	  if (cLogger.isDebugEnabled()) {
		cLogger.debug(lNewTTP.toString());
	}
	  lTask.stopTimer(lTask.getClStartTime() + 122000);
	  JAXBStore.storeXml(lNewTTP, cStrTestFile);
	  
	  lTTP = JAXBStore.readXmlPackage(cStrTestFile);
	  lTask = lTTP.getLatestRunningTask();
	  assert (lTask == null);
	  
  }
}
